//
//  ZNPhotoView.m
//  ZNPhotoView
//
//  Created by zhengn on 16/2/29.
//  Copyright © 2016年 Sniper_Personal. All rights reserved.
//

#import "ZNPhotoView.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
// 需要考虑名字不对时候，考虑pagectrl的左 中  右 时候
@interface ZNPhotoView ()<UIScrollViewDelegate>

/** 滑动相册 */
@property (nonatomic, strong)UIScrollView *scrollView;
/** 页码 */
@property (nonatomic, strong)UIPageControl *pageCtrl;

@property (nonatomic, assign)NSInteger lastIndex;
/** 定时器 轮播用的 */
@property (nonatomic, strong)NSTimer *timer;
@end

@implementation ZNPhotoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    self.scrollView = [[UIScrollView alloc] init];
    [self addSubview:self.scrollView];
    self.scrollView.pagingEnabled = YES;
    // 如果不写这两句的话  self.scrollView的subView里默认有两个滚动条view 再subview的最后面
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor blackColor];
    
    self.pageCtrl = [[UIPageControl alloc] init];
    [self addSubview:self.pageCtrl];
    self.pageCtrl.hidesForSinglePage = YES;
    
    self.maxScale = 0;
    self.minScale = 0;
    self.lastIndex = 0;
    
    // 如果需要自己滚动
//    [self startTimer];
    
}

+ (instancetype)photoViewWithFrame:(CGRect)frame imgArrName:(NSArray *)imgArrName {
    ZNPhotoView *photoView = [[ZNPhotoView alloc] initWithFrame:frame];
    [photoView setImgNameArr:imgArrName];
    return photoView;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.scrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    for (int i = 0; i < self.imgNameArr.count; i++) {
        // 单一scrollview的大小
        UIScrollView *singleScroll = (UIScrollView *)[self.scrollView.subviews objectAtIndex:i];
        singleScroll.frame = CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height);
        singleScroll.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
        
        UIImageView *imgView = (UIImageView *)[[singleScroll subviews] objectAtIndex:0];
        imgView.frame = CGRectMake(0, 0, singleScroll.frame.size.width, singleScroll.frame.size.height);
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        
        if (self.maxScale != 0) {
            singleScroll.maximumZoomScale = self.maxScale;
        }
        if (self.minScale != 0) {
            singleScroll.minimumZoomScale = self.minScale;
        }
    }
    self.pageCtrl.frame = CGRectMake((self.frame.size.width - 60) * 0.5, self.frame.size.height - 40, 60, 40);
}

#pragma mark - property
-(void)setOtherPageColor:(UIColor *)otherPageColor {
    _pageCtrl.pageIndicatorTintColor = otherPageColor;
}

-(void)setCurrentPageColor:(UIColor *)currentPageColor {
    _pageCtrl.currentPageIndicatorTintColor = currentPageColor;
}

-(void)setImgNameArr:(NSArray *)imgNameArr {
    _imgNameArr = imgNameArr;
    // 重复设置数组时,清空一下原来的scrollview
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width * self.imgNameArr.count, self.frame.size.height);
    
    for (int i = 0; i < [_imgNameArr count]; i++) {
        
        UIScrollView *singleScroll = [[UIScrollView alloc] init];
        [self.scrollView addSubview:singleScroll];
        singleScroll.delegate = self;
        singleScroll.showsHorizontalScrollIndicator = NO;
        singleScroll.showsVerticalScrollIndicator = NO;

        UIImageView *imgView = [[UIImageView alloc] init];
        NSString *imgName = [_imgNameArr objectAtIndex:i];
        UIImage *img = nil;
        if (imgName) {
            img = [UIImage imageNamed:imgName];
        }
        [singleScroll addSubview:imgView];
        imgView.image = img;
    }
    
    self.pageCtrl.numberOfPages = _imgNameArr.count;
}

#pragma mark - Timer
- (void)startTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(turnPage) userInfo:nil repeats:YES];
    // 处理UI事件的同时也兼顾一下计时器(比如 滚动其他scrollview 或者textview时候)
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopTimer {
    [self.timer invalidate];
    self.timer = nil;
}

#pragma mark - <UIScrollViewDelegate>
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat index = scrollView.contentOffset.x / self.frame.size.width;
    self.pageCtrl.currentPage = index;
    
    if (self.lastIndex != (NSInteger)(self.contentOffset.x / self.imgNameArr.count)) {
        for (UIScrollView *scrolView in [scrollView subviews]) {
            if ([scrolView isKindOfClass:[UIScrollView class]]) {
                scrolView.zoomScale = 1.0;
            }
        }
    }
    
    self.lastIndex = index;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self stopTimer];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self startTimer];
}

/**
 这个方法的返回值决定了要缩放的内容(返回值职能是UIScrollView的子控件)
 */
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    UIScrollView *scrView = [[scrollView subviews] firstObject];
    
    return scrView;
}

/**
 正在缩放的回调 参数是缩放的scrollview
 */
-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
    for (UIScrollView *scrolView in [scrollView subviews]) {
        if ([scrolView isKindOfClass:[UIImageView class]]) {
            scrolView.center = CGPointMake(scrollView.frame.size.width * 0.5, scrollView.frame.size.height * 0.5);
        }
    }
}

#pragma mark - other
- (void)turnPage {
    [UIView animateWithDuration:0.5 animations:^{
        NSInteger nextPage = self.pageCtrl.currentPage + 1;
        nextPage = nextPage % self.imgNameArr.count;
        
        CGPoint offsetPoint = CGPointMake(nextPage * self.scrollView.frame.size.width, 0);
        self.scrollView.contentOffset = offsetPoint;
        self.pageCtrl.currentPage = nextPage;
    }];
}

@end
